package com.ffcs.crmd.tsp.server.job.info;

import java.io.Serializable;
import java.util.Date;

import org.quartz.JobExecutionException;
import org.quartz.core.JobRunShellTsp;

import com.ffcs.crmd.tsp.api.dto.enu.JobDetailRunStatus;
import com.ffcs.crmd.tsp.api.dto.task.TspJobExecutionException;
import com.ffcs.crmd.tsp.api.task.job.IJobExecutionContext;
import com.ffcs.crmd.tsp.server.util.JobDetailHelper;
import com.thoughtworks.xstream.annotations.XStreamAliasType;

/**
 * 
 * 功能说明:子任务运行内核
 *
 * @author ZHONGFUHUA
 * 
 * @since 2.0.0-SNAPSHOT
 *
 */
@XStreamAliasType("JobDetailShell")
public class JobDetailShell implements Serializable {
    
    /**
     * 
     */
    private static final long                  serialVersionUID = -488442359976782681L;
    
    /**
     * 唯一标识
     */
    private final String                       id               = JobDetailHelper.getId();
    
    /**
     * 子任务名称
     */
    private final String                       jobDetailName;
    
    /**
     * 任务名称
     */
    private final String                       jobName;
    
    /**
     * 集群名称
     */
    private final String                       clusterName;
    
    /**
     * 集群实例ID
     */
    private final String                       instanceId;
    
    /**
     * 子任务执行上下文
     */
    private final IJobExecutionContext<Object> context;
    
    /**
     * quartz任务内核
     */
    private final JobRunShellTsp               jobRunShell;
    
    /**
     * 子任务运行状态
     */
    private volatile JobDetailRunStatus        status;
    
    /**
     * 请求标识
     */
    private volatile int                       remotingOpaque;
    
    /**
     * 上次执行时间
     */
    private volatile Date                      prevTime;
    
    /**
     * 下次执行时间
     */
    private volatile Date                      nextTime;
    
    /**
     * quartz实际触发时间
     */
    private volatile Date                      fireTime;
    
    /**
     * quartz调度时间
     */
    private volatile Date                      scheduledFireTime;
    
    /**
     * 发送时间
     */
    private volatile Date                      sendTime;
    
    /**
     * 执行端接收时间
     */
    private volatile Date                      reciveTime;
    
    /**
     * 完成时间
     */
    private volatile Date                      completeTime;
    
    /**
     * 
     * 功能说明:构造方法（必须用）
     * 
     * @param quartz任务内核
     * @param context 任务执行上下文
     */
    public JobDetailShell(JobRunShellTsp jobRunShell, IJobExecutionContext<Object> context) {
        this.jobRunShell = jobRunShell;
        this.context = context;
        this.fireTime = context.getFireTime();
        this.nextTime = context.getNextFireTime();
        this.prevTime = context.getPreviousFireTime();
        this.scheduledFireTime = context.getScheduledFireTime();
        this.jobDetailName = context.getTspJobInstance().getName();
        this.jobName = context.getTspJobInstance().getJobName();
        this.clusterName = context.getSchedulerName();
        this.instanceId = context.getFireInstanceId();
        this.status = JobDetailRunStatus.FIRE;
    }
    
    /**
     * 
     * 功能说明:子任务结束回调异常判断
     * 
     * @throws TspJobExecutionException
     */
    public void callBack(JobExecutionException jee) {
        jobRunShell.setJobExEx(jee);
        jobRunShell.completeJob();
    }
    
    /**
     * 
     * 功能说明:子任务结束回调异常判断
     * 
     * @throws TspJobExecutionException
     */
    public void callBack() {
        jobRunShell.completeJob();
    }
    
    /**
     * 
     * 功能说明:设置发送时间
     *
     */
    public void updateSendTime() {
        this.sendTime = new Date();
        this.status = JobDetailRunStatus.SEND;
    }
    
    /**
     * 
     * 功能说明:更新触发时间
     *
     */
    public void updateFireTime() {
        this.sendTime = null;
        this.reciveTime = null;
        this.completeTime = null;
        this.status = JobDetailRunStatus.FIRE;
    }
    
    /**
     * 功能说明: 设置执行端接收时间
     *
     */
    public void updateReciveTime(Date date) {
        this.reciveTime = date;
        this.status = JobDetailRunStatus.RECIVE;
    }
    
    /**
     * 功能说明: 设置完成时间
     *
     * @param completeTime 完成时间
     */
    public void updateCompleteTime() {
        this.completeTime = new Date();
        this.status = JobDetailRunStatus.COMPLETE;
    }
    
    /**
     * 
     * 功能说明:更新状态为离线
     *
     */
    public void updateOffline() {
        if (this.status == JobDetailRunStatus.TIMEOUT) {
            this.status = JobDetailRunStatus.OFFLINE_TIMEOUT;
        } else {
            this.status = JobDetailRunStatus.OFFLINE;
        }
    }
    
    /**
     * 
     * 功能说明:更新状态为离线
     *
     */
    public void updateOnline() {
        if (this.status == JobDetailRunStatus.OFFLINE_TIMEOUT) {
            this.status = JobDetailRunStatus.TIMEOUT;
        } else {
            this.status = JobDetailRunStatus.RECIVE;
        }
    }
    
    /**
     * 
     * 功能说明:更新状态为超时
     *
     */
    public void updateTimeout() {
        if (this.status == JobDetailRunStatus.OFFLINE) {
            this.status = JobDetailRunStatus.OFFLINE_TIMEOUT;
        } else {
            this.status = JobDetailRunStatus.TIMEOUT;
        }
    }
    
    /**
     * 功能说明: 获取上次执行时间
     *
     * @return prevTime 上次执行时间
     */
    public Date getPrevTime() {
        return prevTime;
    }
    
    /**
     * 功能说明: 获取下次执行时间
     *
     * @return nextTime 下次执行时间
     */
    public Date getNextTime() {
        return nextTime;
    }
    
    /**
     * 功能说明: 获取触发时间
     *
     * @return fireTime 触发时间
     */
    public Date getFireTime() {
        return fireTime;
    }
    
    /**
     * 功能说明: 获取调度触发时间
     *
     * @return scheduledFireTime 调度触发时间
     */
    public Date getScheduledFireTime() {
        return scheduledFireTime;
    }
    
    /**
     * 功能说明: 获取执行接收时间
     *
     * @return returnTime 执行端接收时间
     */
    public Date getReciveTime() {
        return reciveTime;
    }
    
    /**
     * 功能说明: 获取完成时间
     *
     * @return completeTime 完成时间
     */
    public Date getCompleteTime() {
        return completeTime;
    }
    
    /**
     * 功能说明: 设置发送时间
     *
     * @param sendTime 发送时间
     */
    public void setSendTime(Date sendTime) {
        this.sendTime = sendTime;
    }
    
    /**
     * 
     * 功能说明:获取发送时间
     * 
     * @return 发送时间
     */
    public Date getSendTime() {
        return sendTime;
    }
    
    /**
     * 功能说明: 获取请求标识
     *
     * @return remotingOpaque 请求标识
     */
    public int getRemotingOpaque() {
        return remotingOpaque;
    }
    
    /**
     * 功能说明: 设置请求标识
     *
     * @param remotingOpaque 请求标识
     */
    public void setRemotingOpaque(int remotingOpaque) {
        this.remotingOpaque = remotingOpaque;
    }
    
    /**
     * 功能说明: 获取子任务运行状态
     *
     * @return status 子任务运行状态
     */
    public JobDetailRunStatus getStatus() {
        return status;
    }
    
    /**
     * 
     * 功能说明:获取任务执行上下文
     * 
     * @return context 任务执行上下文
     */
    public IJobExecutionContext<Object> getContext() {
        return context;
    }
    
    /**
     * 功能说明: 获取任务执行脚本
     *
     * @return jobRunShell 任务执行脚本
     */
    public JobRunShellTsp getJobRunShell() {
        return jobRunShell;
    }
    
    /**
     * 功能说明: 获取id
     *
     * @return id id
     */
    public String getId() {
        return id;
    }
    
    /**
     * 功能说明: 获取子任务名称
     *
     * @return jobDetailName 子任务名称
     */
    public String getJobDetailName() {
        return jobDetailName;
    }
    
    /**
     * 功能说明: 获取任务名称
     *
     * @return jobName 任务名称
     */
    public String getJobName() {
        return jobName;
    }
    
    /**
     * 功能说明: 获取集群名称
     *
     * @return clusterName 集群名称
     */
    public String getClusterName() {
        return clusterName;
    }
    
    /**
     * 功能说明: 获取集群实例ID
     *
     * @return instanceId 集群实例ID
     */
    public String getInstanceId() {
        return instanceId;
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @return
     */
    public String getJobKey() {
        return this.jobName + "." + this.jobDetailName;
    }
    
    /**
     * 功能说明:
     * 
     * @return
     */
    @Override
    public String toString() {
        return "JobDetailShell [id=" + id + ", jobDetailName=" + jobDetailName + ", jobName=" + jobName + ", remotingOpaque=" + remotingOpaque
            + ", status=" + status + ", prevTime=" + prevTime + ", nextTime=" + nextTime + ", fireTime=" + fireTime + ", scheduledFireTime="
            + scheduledFireTime + ", sendTime=" + sendTime + ", reciveTime=" + reciveTime + ", completeTime=" + completeTime + "]";
    }
    
}
